use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "data_items")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
    pub description: Option<String>,
    pub project: Option<String>,
    pub source: String,
    pub file_path: String,
    pub file_hash: Option<String>,
    pub file_size: Option<i64>,
    pub file_type: Option<String>,
    pub is_folder: bool,
    pub file_count: Option<i64>,
    pub user_id: i64,
    pub parent_id: Option<i64>,
    pub is_public: bool,
    pub created_at: DateTime,
    pub updated_at: DateTime,
    pub dvc_metadata_id: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::users::Entity",
        from = "Column::UserId",
        to = "super::users::Column::Id"
    )]
    User,
    #[sea_orm(
        belongs_to = "super::data_items::Entity",
        from = "Column::ParentId",
        to = "super::data_items::Column::Id"
    )]
    Parent,
    #[sea_orm(
        belongs_to = "super::dvc_metadata::Entity",
        from = "Column::DvcMetadataId",
        to = "super::dvc_metadata::Column::Id"
    )]
    DvcMetadata,
    #[sea_orm(has_many = "super::data_items::Entity")]
    Children,
}

impl Related<super::users::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::User.def()
    }
}

impl Related<super::data_items::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Parent.def()
    }
}

impl Related<super::dvc_metadata::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::DvcMetadata.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}
